package com.app.newiptv.adaper;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.app.newiptv.R;
import com.app.newiptv.activity.VodActivity;
import com.app.newiptv.base.MyApplication;
import com.app.newiptv.presenter.VodPresenter;
import com.app.newiptv.utils.FocusedViewUtils;
import com.app.newiptv.utils.Paths;
import com.bumptech.glide.Glide;


/**
 * Created by wendal on 2017/9/16.
 */

public class VodItemAdapter extends RecyclerView.Adapter<VodItemAdapter.MyViewHolder> {
    private VodPresenter presenter;
    private int count;
    private VodActivity mActivity;
    private RecyclerView parentRecycle;
    public View currentView;
    private View focusview;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
                FocusedViewUtils.setFocusedView(focusview, currentView, 25, 15, 30, 15);
        }
    };
    public VodItemAdapter(VodPresenter presenter, VodActivity activity,View view) {
        this.presenter = presenter;
        this.mActivity = activity;
        this.focusview = view;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.moive_item, null);
        MyViewHolder viewHolder = new MyViewHolder(view);
        if (parentRecycle == null) {
            parentRecycle = (RecyclerView) parent;
            parentRecycle.addOnScrollListener(new MovieWorldScrollListener());
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        presenter.onBindItem(holder, position);
        holder.setPositon(position);
    }

    @Override
    public int getItemCount() {
        return count;
    }
    class MyViewHolder extends RecyclerView.ViewHolder implements VodPresenter.ItemViewHolder {
        private ImageView item_image;
        private TextView text_naem;
        private int positon;

        public MyViewHolder(View itemView) {
            super(itemView);
            item_image = (ImageView) itemView.findViewById(R.id.iv_movie_world);
            text_naem = (TextView) itemView.findViewById(R.id.tv_movie_world);
            text_naem.setTypeface(MyApplication.face);
            itemView.setOnFocusChangeListener(new SetOnFocuse());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    presenter.setPosition(positon);
                }
            });
        }

        @Override
        public void setImage(String imageUrl) {
            Glide.with(mActivity).load(Paths.HOSTPATH + imageUrl).into(item_image);
        }

        @Override
        public void setItemName(String name) {
            text_naem.setText(name);
        }

        public void setPositon(int positon) {
            this.positon = positon;
        }

        class SetOnFocuse implements View.OnFocusChangeListener {

            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    if(handler.hasMessages(0)){
                        handler.removeMessages(0);
                    }
                    currentView = item_image;
                    boolean scroll = FocusedViewUtils.arrowScroll(view);
                    if(!scroll) {
                        FocusedViewUtils.setFocusedView(focusview,item_image,25,15,30,15);
                    }else{
                        handler.sendEmptyMessageDelayed(0,300);
                    }
                    mActivity.select_position.setText((positon+1) + "/" + count+" | 按右键可以快速选择分类");
                } else {
                    FocusedViewUtils.setBlurView(focusview,item_image);
                    //item_image.animate().scaleX(1.0f);
                    //item_image.animate().scaleY(1.0f);
                }
            }
        }
    }
    class MovieWorldScrollListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                //if(focusview!=null&&currentView!=null) {
                //    FocusedViewUtils.setFocusedView(focusview, currentView, 25, 15, 30, 15);
                //}
            }
        }
    }
}
